﻿using System;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace DataAccess.Tests
{
    [TestClass()]
    public class PostgreSql93InOutParametersTest
    {
        private TestContext testContextInstance;

        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
        }
        
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        readonly string connectionName = "PostgreSQLDataAccessTest.InOutParametersTest.ConnectionString";

        internal class Message
        {
            public int MessageId { get; set; }
            public string Text { get; set; }
        }

        [TestMethod()]
        public void InOutParametersExecuteTest()
        {
            // Test script executor (create database)
            ScriptExecutor.ExecuteScript(ConnectionManager.GetConnection("PostgreSQLMaster"),
@"
DROP DATABASE IF EXISTS ""InOutParametersTest"";

CREATE DATABASE ""InOutParametersTest""
  WITH OWNER = ""user""
       ENCODING = 'UTF8'
       TABLESPACE = pg_default
       LC_COLLATE = 'English_United States.1252'
       LC_CTYPE = 'English_United States.1252'
       CONNECTION LIMIT = -1;
",
            ";");

            ScriptExecutor.ExecuteScript(ConnectionManager.GetConnection(connectionName),
@"
CREATE OR REPLACE FUNCTION TestInOutParameters(
    OUT outParameter INT, 
    INOUT inOutparameter VARCHAR(50), 
    OUT outGuidParameter UUID) 
RETURNS SETOF record AS 
$$
BEGIN

	outParameter := 543;
	
	IF inOutparameter IS NULL
	THEN
		inOutparameter := 'Message 2';
	END IF;
		
	outGuidParameter := 'A0EEBC99-9C0B-4EF8-BB6D-6BB9BD380A11';
		
	RETURN QUERY SELECT 987;

END
$$ 
LANGUAGE plpgsql;
",
                        null);

            var cmd = Factory
                .Command()
                .Connection(connectionName)
                .StoredProcedure("TestInOutParameters")
                .Parameter(
                    Factory
                        .Parameter("outParameter")
                        .IsOutput()
                        .Value(1)
                 )
                 .Parameter(
                    Factory
                        .Parameter("inOutParameter")
                        .IsInputOutput()
                        .Size(50)
                 )
                 .Parameter(
                    Factory
                        .Parameter("outGuidParameter")
                        .IsOutput()
                        .Value(Guid.Empty)
                 );

            cmd.Execute();

            Assert.AreEqual(543, cmd.ParameterValue(0));
            Assert.AreEqual("Message 2", cmd.ParameterValue(1));
            Assert.AreNotEqual(Guid.Empty, cmd.ParameterValue(2));
            Assert.AreEqual(987, cmd.ReturnCode);
        }
    }
}
